/**
 * 认证相关 API
 */
import { post } from '../utils/request.js'
import { API_ENDPOINTS } from '../utils/config.js'
import { encryptPassword } from '../utils/auth.js'

/**
 * 用户登录
 * @param {string} username 用户名
 * @param {string} password 密码
 * @returns {Promise} 登录结果
 */
export const login = (username, password) => {
  return post(API_ENDPOINTS.AUTH.LOGIN, {
    username,
    passwordMd5: encryptPassword(password)
  }, {
    isLoginRequest: true  // 标识这是登录请求，避免401错误时自动跳转
  })
}

/**
 * 用户注册
 * @param {string} username 用户名
 * @param {string} password 密码
 * @returns {Promise} 注册结果
 */
export const register = (username, password) => {
  return post(API_ENDPOINTS.AUTH.REGISTER, {
    username,
    passwordMd5: encryptPassword(password)
  })
}

/**
 * 用户登出
 * @returns {Promise} 登出结果
 */
export const logout = () => {
  return post(API_ENDPOINTS.AUTH.LOGOUT)
}
